package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yuwj.chinaa.R;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.view.PhotoViewPager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import uk.co.senab.photoview.PhotoView;

/**
 * 警情图片编辑页面
 */
public class ImageShowActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.pvp_image)
    PhotoViewPager pvpImage;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_description)
    TextView tvDescription;

    //    BaseCommonAdapter<String> adapterImages;
    PagerAdapter adapterViewPager;
    List<String> imageDatas;
    List<View> imageViews;
    int currentPageviewIndex;
    String description;

    public static Intent createIntent(Context context, List<String> imageDatas, int index) {
        Intent intent = new Intent(context, ImageShowActivity.class);
        intent.putExtra("imageDatas", (Serializable) imageDatas);
        intent.putExtra("index", index);
        return intent;
    }

    public static Intent createIntent(Context context, List<String> imageDatas, int index, String description) {
        Intent intent = new Intent(context, ImageShowActivity.class);
        intent.putExtra("imageDatas", (Serializable) imageDatas);
        intent.putExtra("index", index);
        intent.putExtra("description", description);
        return intent;
    }

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_image_show;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initParam();
        initView();
        initOperator();
    }


    public List<View> createViewpagerDatas(List<String> imageDatas) {
        List<View> views = new ArrayList<>();

        for (String s : imageDatas) {
            ImageView iv = new PhotoView(context);
//            iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
            Glide.with(context)
                    .load(s)
//                    .placeholder(R.mipmap.ic_handlejingq_wait_img)
                    .crossFade()
                    .into(iv);
            views.add(iv);
        }
        return views;
    }

    public void initParam() {
        Intent intent = getIntent();
        imageDatas = (List<String>) intent.getSerializableExtra("imageDatas");
        currentPageviewIndex = intent.getIntExtra("index", 0);
        imageViews = new ArrayList<>();
        imageViews.addAll(createViewpagerDatas(imageDatas));
        adapterViewPager = createViewPagerAdapter(imageViews);

    }

    public PagerAdapter createViewPagerAdapter(final List<View> imageViews) {
        PagerAdapter adapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return imageViews.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(imageViews.get(position));
                return imageViews.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                if (position > imageViews.size() - 1) return;
                container.removeView(imageViews.get(position));
            }

            @Override
            public int getItemPosition(Object object) {
                return POSITION_NONE;
            }
        };
        return adapter;
    }

    public void initView() {
        initActionBar(toolbar, tvTitle, "图片详情");
        pvpImage.setAdapter(adapterViewPager);
        pvpImage.setCurrentItem(currentPageviewIndex);
        if (!TextUtils.isEmpty(description)) {
            tvDescription.setText(description + "");
        } else {
            tvDescription.setVisibility(View.GONE);
        }
    }


    public void initOperator() {
    }


}
